from sqlalchemy import create_engine,func
from sqlalchemy.orm import sessionmaker
from Tables import a01
from dofx import dofx

import math


class getData:
    def __init__(self):
        self.engine1=create_engine('mysql+pymysql://root:admin@127.0.0.1:35017/hy_qggwy')
        self.engine2=create_engine('mysql+pymysql://root:admin@127.0.0.1:35017/nlpfx')        
        self.session1=None
        self.session2=None
        self.count = 0
        self.index = 0


    def start(self):
        DBSession1 = sessionmaker(bind=self.engine1)
        self.session1 = DBSession1()
        DBSession2 = sessionmaker(bind=self.engine2)
        self.session2 = DBSession2()        
        # 创建 session 对象:
        self.count =self.session1.query(func.count(a01.a0000)).scalar()
        pageindex = math.ceil(self.count/50)
        for i in range(pageindex):
            self.query(pageIndex=i+1)

    def query(self,pageSize=50,pageIndex=1):
        rows_1=self.session1.query(a01).order_by(a01.a0000.desc()).limit(pageSize).offset((pageIndex-1)*pageSize).all()
        for row in rows_1:
            self.index += 1
            print("当前数:",self.index,",总数：",self.count,"百分比：",'percent: {:.2%}'.format(self.index/self.count))
            person = row.to_dict()
            d = dofx()
            d.qgjz(person['a0000'],person['A0101'],person['A1701'],person['A0107'],person['A0184'],self.session2)
